import axios from 'axios'
export default {
  data () {
    return {
      loading3: false,
      loading4: false,
      current: 1,
      size: 10,
      total: 0,
      modal1: false,
      gallery: [],
      parentIdList: [],

      tableData2: [],
      imgurl1: '',
      imgurl2: [],
      tableColumn2: [
        {
          label: '展览名称',
          prop: 'name'
        },
        {
          label: '展览类型',
          prop: 'exhibitionTypeName'
        },
        {
          label: '权重',
          prop: 'weight'
        }
      ],
      name: '',
      exhibitionId: '',
      loading2:false,
      modal2: false,
      newData2: {
        about: '',
        exhibitionTypeId: '1',
        gallery: [],
        name: '',
        parentId: '0',
        pic: '',
        weight: ''
      },
      exhibitionTypeList: [],
      id2: '',
      pid: '0',

      modal3: false,
      ind: 0,
      clear: false,
      exhibitionList: [],
      loading7: false,
      ids: []
    }
  },
  created () {
    this.getExhibitionHallList()
    this.getExhibitionList()
    this.getExhibitionTypeList()
  },
  methods: {
    show (data) {
      this.gallery = []
      this.gallery = data
      this.modal1 = true
      console.log(data)
    },
    nameChange () {
      this.current = 1
      this.getExhibitionHallList()
    },
    pidChange () {
      this.current = 1
      this.getExhibitionHallList()
    },
    getExhibitionTypeList () {
      this.$http('/ztmuseum/stage/exhibition-type/selector', {}, 'get', (res) => {
        console.log(res)
        if (res.data.code == 1000) {
          this.exhibitionTypeList = res.data.data
        } else {
          this.$Message.error(res.data.msg)
        }
      })
    },
    getExhibitionList () {
      this.$http('/ztmuseum/stage/exhibition/selector', {}, 'get', (res) => {
        console.log(res)
        if (res.data.code == 1000) {
          this.exhibitionList = res.data.data
        } else {
          this.$Message.error(res.data.msg)
        }
      })
    },
    showChilren (id) {
      this.loading3 = true
      this.current = 1
      this.pid = id
      this.$http('/ztmuseum/stage/exhibition/getExhibitionList', {
        current: this.current,
        size: this.size,
        pid: this.pid,
        name: this.name
      }, 'post', (res) => {
        if (res.data.code == 1000) {
          this.tableData2 = res.data.data.records
          this.total = res.data.data.total
        } else {
          this.$Message.error(res.data.msg)
        }
        this.loading3 = false
      })
    },
    changePage (data) {
      this.current = data
      this.getExhibitionHallList()
    },
    handel (data) {
      console.log(data)
      this.newData2 = {
        about: data.about,
        exhibitionTypeId: data.exhibitionTypeId,
        gallery: data.gallery,
        name: data.name,
        parentId: data.parentId,
        pic: data.pic,
        weight: data.weight
      }
      this.id2 = data.id
      this.imgurl1 = data.pic
      this.imgurl2 = []
      data.gallery.map((item) => {
        this.imgurl2.push({
          name: item,
          percentage: 100,
          status: 'finished',
          uid: item,
          url: item
        })
      })
      this.modal2 = true
    },
    getExhibitionHallList () {
      this.loading3 = true
      this.$http('/ztmuseum/stage/exhibition/getExhibitionList', {
        current: this.current,
        size: this.size,
        pid: this.pid,
        name: this.name
      }, 'post', (res) => {
        console.log(res)
        if (res.data.code == 1000) {
          this.tableData2 = res.data.data.records
          this.total = res.data.data.total
        } else {
          this.$Message.error(res.data.msg)
        }
        this.loading3 = false
      })
    },
    allPush(){
      this.loading2=true
      if(this.ids.length==0){
        this.$Message.error('请勾选要导出的展览')
        this.loading2=false
        return false
      }else{
        let that = this
      this.$confirm('此操作将批量导出展览二维码， 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        showClose: false,
        type: 'warning'
      }).then(() => {
        axios({
          method: 'post',
          url: this.baseUrl + '/ztmuseum/stage/exhibition/exportQrCode',
          headers: {
            'token': this.$store.state.token,
            'Content-Type': 'application/json;charset=UTF-8'
          },
          responseType: 'blob',
          data: {
            ids: this.ids
          }
        }).then(function (res) {
          console.log(res)
          that.loading2 = false
          let blob = new Blob([res.data], {
            type: 'application/x-zip-compressed'
          })
          let objectUrl = URL.createObjectURL(blob)
          let a = document.createElement('a')
          a.href = objectUrl
          a.download = '展览二维码'
          a.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, view: window }))
          window.URL.revokeObjectURL(blob)
        }).catch(resp => {
          that.loading2 = false
          that.$Message.error('导出二维码失败')
        })
      }).catch(() => {
        this.loading7 = false
      })
      }

    },
    del2 (data) {
      // console.log(data);
      if (data.hasLeaf) {
        this.$confirm('此操作将删除该展展览,以及该展展览下面的展览全部会删除，请谨慎操作，是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          showClose: false,
          type: 'warning'
        }).then(() => {
          this.$http('/ztmuseum/stage/exhibition/deleteExhibition', {
            id: data.id
          }, 'post', (res) => {
            if (res.data.code == 1000 && res.data.data) {
              this.$Message.success('删除成功')
              this.current = 1
              this.getExhibitionHallList()
              // this.getBottomExhibitionHallDropDown()
            } else {
              this.$Message.error(res.data.msg)
            }
            this.modal3 = false
          })
        }).catch(() => {

        })
      } else {
        this.$confirm('此操作将删除该展览， 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          showClose: false,
          type: 'warning'
        }).then(() => {
          this.$http('/ztmuseum/stage/exhibition/deleteExhibition', {
            id: data.id
          }, 'post', (res) => {
            if (res.data.code == 1000 && res.data.data) {
              this.$Message.success('删除成功')
              this.current = 1
              this.getExhibitionHallList()
              // this.getBottomExhibitionHallDropDown()
            } else {
              this.$Message.error(res.data.msg)
            }
            this.modal3 = false
          })
        }).catch(() => {

        })
      }
    },
    create () {
      this.ind++
      this.newData2 = {
        about: '',
        exhibitionTypeId: '1',
        gallery: [],
        name: '',
        parentId: '0',
        pic: '',
        weight: ''
      }
      this.modal2 = true
      this.id2 = ''
      this.imgurl1 = ''
      this.clear = true
      this.imgurl2 = []
    },
    submit2 () {
      this.$refs['ruleForm2'].validate((valid) => {
        if (valid) {
          this.loading4 = true
          console.log(this.newData2)
          if (this.id2 == '') {
            this.$http('/ztmuseum/stage/exhibition/addExhibition', {
              about: this.newData2.about,
              name: this.newData2.name,
              weight: this.newData2.weight,
              parentId: this.newData2.parentId,
              exhibitionTypeId: this.newData2.exhibitionTypeId,
              gallery: this.newData2.gallery,
              pic: this.newData2.pic
            }, 'post', (res) => {
              if (res.data.code == 1000 && res.data.data) {
                this.$Message.success('新增成功')
                this.getExhibitionHallList()
              } else {
                this.errorMessage(res.data.msg)
              }
              this.modal2 = false
              this.loading4 = false
            })
          } else {
            this.$http('/ztmuseum/stage/exhibition/updateExhibition', {
              about: this.newData2.about,
              name: this.newData2.name,
              weight: this.newData2.weight,
              parentId: this.newData2.parentId,
              exhibitionTypeId: this.newData2.exhibitionTypeId,
              gallery: this.newData2.gallery,
              pic: this.newData2.pic,
              id: this.id2
            }, 'post', (res) => {
              this.loading4 = false
              if (res.data.code == 1000 && res.data.data) {
                this.$Message.success('修改成功')
                this.getExhibitionHallList()
              } else {
                this.$Message.error(res.data.msg)
              }
              this.modal2 = false
            })
          }
        }
      })
    },
    selectionChange(seletion){
      console.log(seletion);
      this.ids = []
      seletion.map(item=>{
        this.ids.push(item.id)
      })
    },
    catchData2 (data) {
      this.newData2.about = data
    },
    uploadImg1 (data) {
      console.log(data)
      this.newData2.pic = data
      console.log(this.imgurl1)
      this.imgurl1 = data
    },
    uploadImg2 (data) {
      this.newData2.gallery = []
      this.imgurl2 = []
      data.map(item => {
        this.newData2.gallery.push(item.url)
      })

      this.imgurl2 = data
      console.log(data, 1111)
    },
    download (data) {
      this.ids = [data.id]
      let that = this
      this.loading7 = true
      this.$confirm('此操作将导出该展览二维码， 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        showClose: false,
        type: 'warning'
      }).then(() => {
        axios({
          method: 'post',
          url: this.baseUrl + '/ztmuseum/stage/exhibition/exportQrCode',
          headers: {
            'token': this.$store.state.token,
            'Content-Type': 'application/json;charset=UTF-8'
          },
          responseType: 'blob',
          data: {
            ids: this.ids
          }
        }).then(function (res) {
          console.log(res)
          that.loading7 = false
          let blob = new Blob([res.data], {
            type: 'application/x-zip-compressed'
          })
          let objectUrl = URL.createObjectURL(blob)
          let a = document.createElement('a')
          a.href = objectUrl
          a.download = '展览二维码'
          a.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, view: window }))
          window.URL.revokeObjectURL(blob)
          this.ids=[]
        }).catch(resp => {
          that.loading7 = false
          that.$Message.error('导出二维码失败')
          this.ids=[]
        })
      }).catch(() => {
        this.loading7 = false
        this.ids=[]
      })

      // this.modal4 = true
      // this.bottomWords = '微信扫一扫,听语音讲解'
    }

  }
}
